/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vladimirkolarevic.eventplanner.tapestry.pages;

import com.vladimirkolarevic.eventplanner.Attend;
import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.UserBean;
import com.vladimirkolarevic.eventplanner.service.AttendService;
import com.vladimirkolarevic.eventplanner.service.MembershipsService;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 * 
 * @author Vlada
 */
public class UserInfo {

	@InjectPage
	private EventInfo eventInfo;

	@Property
	@ApplicationState
	private User user;
	private boolean userExists;

	@InjectPage
	private ChangeUserInfo changeinfo;

	private List<Event> events;

	@Property
	private Event event;

	@Inject
	private MembershipsService membership;

	@Inject
	private AttendService attendService;

	private List<Event> addedEvents;

	@Property
	private Event addedEvent;

	@BeginRender
	public void setUpObjects() {

		addedEvents = membership.findUser(user.getId()).getEvents();

		Attend a = attendService.retrive(user);
		if (a != null) {
			events = a.getEvents();
		}

	}

	public Object onActivate() {
		if (userExists) {
			return null;
		}
		return Index.class;
	}

	@OnEvent(component = "eventLink")
	public Object showEventInfo(Long id) {
		eventInfo.setId(id);
		return eventInfo;
	}

	public List<Event> getAllEvents() {
		return events;
	}

	public List<Event> getAddedEvents() {
		return addedEvents;
	}

	@OnEvent(component = "addedEventLink")
	public Object showAddedEventInfo(Long id) {
		eventInfo.setId(id);
		return eventInfo;
	}

}
